﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using Pharmacy.Data;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;
using Pharmacy.Entities;

public partial class Admin_PreviewRevenue : System.Web.UI.Page
{
    DateTime tmpfromdate, tmptodate, fromdate, todate;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["From"] != null && Request.QueryString["To"] != null)
                {
                    tmpfromdate = DateTime.Parse(Request.QueryString["From"].ToString());
                    tmptodate = DateTime.Parse(Request.QueryString["To"].ToString());
                    fromdate = new DateTime(tmpfromdate.Year, tmpfromdate.Month, tmpfromdate.Day, 0, 0, 0);
                    todate = new DateTime(tmptodate.Year, tmptodate.Month, tmptodate.Day, 23, 59, 59);
                    PreviewPo(fromdate,todate);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    private void PreviewPo(DateTime from,DateTime to)
    {
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "_PO_NotComplete";
        cmd.CommandTimeout = 0;
        cmd.Parameters.Add("@fromdate", SqlDbType.DateTime).Value = from;
        cmd.Parameters.Add("@todate", SqlDbType.DateTime).Value = to;
        ds = DataRepository.Provider.ExecuteDataSet(cmd);
        DataTable ntablePo = ds.Tables[0];

        DSPoNotComplete.PoDetailsDataTable dtPO = new DSPoNotComplete.PoDetailsDataTable();
        DSPoNotComplete.PoDetailsRow dtPoDetailRow;
        for (int i = 0; i < ntablePo.Rows.Count; i++)
        {
            dtPoDetailRow = dtPO.NewPoDetailsRow();
            DataRow drw = ntablePo.Rows[i];
            dtPoDetailRow.PoId = drw["PoId"].ToString();
            dtPoDetailRow.PoDetailId = drw["PoDetailId"].ToString();
            dtPoDetailRow.DrugId = drw["DrugId"].ToString();
            dtPoDetailRow.DrugName = drw["DrugName"].ToString();
            dtPoDetailRow.Unit =drw["Unit"].ToString();
            dtPoDetailRow.RemainQuantity = int.Parse(drw["RemainQuantity"].ToString());
            dtPoDetailRow.Remark = drw["Remark"].ToString();
            dtPO.AddPoDetailsRow(dtPoDetailRow);
        }

        DSPoNotComplete.TitleDataTable dtTitle = new DSPoNotComplete.TitleDataTable();
        DSPoNotComplete.TitleRow dtTitleRow = dtTitle.NewTitleRow();
        dtTitleRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtTitleRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;
        //get prepareby from cookies
        var httpCookie = Request.Cookies["LoginUser"];
        WebUser id = DataRepository.WebUserProvider.GetByUserId(httpCookie.Value);
        dtTitleRow.PrepareBy = id.Name;
        dtTitleRow.Fromdate = from;
        dtTitleRow.Todate = to;
        dtTitle.AddTitleRow(dtTitleRow);

        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSPoNotComplete_PoDetails", dtPO));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSPoNotComplete_Title", dtTitle));
        ReportViewer1.DataBind();
    }
}
